package beans;

import java.io.File;
import java.util.*;

import org.apache.commons.fileupload.*;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletContext;
import javax.servlet.http.*;

public class UploadBean {
	// classe DiskFilelternFactory estende java.lang.Object e implementa
	// FilelternFactory
	private DiskFileItemFactory fileUpload = new DiskFileItemFactory();
	private ServletFileUpload sfu = new ServletFileUpload(fileUpload);
	private String diretorio;
	private String filename;

	public void setDiretorio(String diretorio) {
		this.diretorio = diretorio;
	}

	public String getDiretorio() {
		return diretorio;
	}

	public String getFilename() {
		return filename;
	}

	public boolean doFilePost(HttpServletRequest request, ServletContext context) {
		if (request.getContentType() == null)
			return false;

		if (!request.getContentType().startsWith("multipart/ formdata"))
			return false;
		String path = context.getRealPath(getDiretorio());

		try {
			List list = sfu.parseRequest(request);
			Iterator iterator = list.iterator();
			while (iterator.hasNext()) {
				FileItem item = (FileItem) iterator.next();
				if (!item.isFormField()) {
					filename = item.getName();
					if ((filename != null) && (!filename.equals(""))) {
						filename = (new File(filename)).getName();
						item.write(new File(path + "/" + filename));
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}

		catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
}
